//
//  EmotionDao.swift
//  Tinsecret_TW
//
//  Created by wanghui on 2018/11/28.
//  Copyright © 2018年 tst. All rights reserved.
//

import Foundation
import WCDBSwift

class EmotionDao {
    
    // 获取普通表情组 中所有的表情
    static func getAllEmotions(groupId:Int64) throws -> [Emotion] {
        var returnData = [Emotion]()
        let db = SQLiteHelper.getDb()
        let emotions:[Emotion] = try db.getObjects(on: Emotion.CodingKeys.all, fromTable: Emotion.table, where: Emotion.CodingKeys.groupID == groupId)
        returnData = emotions
        return returnData
    }

    //获取 FavoritesGroup 表中所有的表情
    static func getAllEmotionsInFavoritesGroup() throws -> [Emotion] {
        var returnData = [Emotion]()
        let db = SQLiteHelper.getDb()
        let emotions:[Emotion] = try db.getObjects(on: Emotion.CodingKeys.all, fromTable: FavoritesGroup.table)
        returnData = emotions
        return returnData
    }
    
    static func getEmotion(id:Int64) throws -> Emotion? {
        var returnData:Emotion?
        let db = SQLiteHelper.getDb()
        let emotion:Emotion? = try db.getObject(on: Emotion.CodingKeys.all, fromTable: Emotion.table, where: Emotion.CodingKeys.id == id)
        returnData = emotion
        return returnData
    }
    
    static func save(emotion:Emotion) throws {
        let db = SQLiteHelper.getDb()
        try db.insert(emotion, intoTable: Emotion.table)
    }
    
    static func delete(id:Int64) throws {
        let db = SQLiteHelper.getDb()
        try db.delete(fromTable: Emotion.table, where: Emotion.CodingKeys.id == id)
    }
    
    static func delete(thumbnail:String) throws {
        let db = SQLiteHelper.getDb()
        try db.delete(fromTable: Emotion.table, where: Emotion.CodingKeys.thumbnailUrl == thumbnail)
    }
    
    static func delete(groupID:Int64) throws {
        let db = SQLiteHelper.getDb()
        try db.delete(fromTable: Emotion.table, where: Emotion.CodingKeys.groupID == groupID)
    }
}
